package org.example.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.example.system.common.model.Option;
import org.example.system.common.result.Result;
import org.example.system.model.form.MenuForm;
import org.example.system.model.query.MenuQuery;
import org.example.system.model.vo.MenuVO;
import org.example.system.model.vo.RouteVO;
import org.example.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import org.example.system.service.SysMenuService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单控制器
 * 处理菜单相关的请求
 * 包括获取菜单列表、路由列表、菜单选项、菜单表单数据，以及新增、修改和删除菜单
 *
 * @author LY
 */
@Tag(name = "04.菜单接口")
@RestController
@RequestMapping("/api/v1/menus")
@RequiredArgsConstructor
@Slf4j
public class SysMenuController {

    private final SysMenuService menuService;

    // 权限常量
    private static final String PERMISSION_MENU_ADD = "sys:menu:add";
    private static final String PERMISSION_MENU_EDIT = "sys:menu:edit";
    private static final String PERMISSION_MENU_DELETE = "sys:menu:delete";

    /**
     * 获取菜单列表
     *
     * @param queryParams 查询参数
     * @return 菜单列表
     */
    @Operation(summary = "菜单列表")
    @GetMapping
    public Result<List<MenuVO>> listMenus(@ParameterObject MenuQuery queryParams) {
        List<MenuVO> menuList = menuService.listMenus(queryParams);
        return Result.success(menuList);
    }

    /**
     * 获取路由列表
     *
     * @return 路由列表
     */
    @Operation(summary = "路由列表")
    @GetMapping("/routes")
    public Result<List<RouteVO>> listRoutes() {
        List<RouteVO> routeList = menuService.listRoutes();
        return Result.success(routeList);
    }

    /**
     * 获取菜单下拉列表
     *
     * @return 菜单下拉选项列表
     */
    @Operation(summary = "菜单下拉列表")
    @GetMapping("/options")
    public Result<List<Option<Long>>> listMenuOptions() {
        List<Option<Long>> menus = menuService.listMenuOptions();
        return Result.success(menus);
    }

    /**
     * 获取菜单表单数据
     *
     * @param id 菜单ID
     * @return 菜单表单数据
     */
    @Operation(summary = "菜单表单数据")
    @GetMapping("/{id}/form")
    public Result<MenuForm> getMenuForm(@Parameter(description = "菜单ID") @PathVariable Long id) {
        MenuForm menu = menuService.getMenuForm(id);
        return Result.success(menu);
    }

    /**
     * 新增菜单
     *
     * @param menuForm 菜单表单数据
     * @return 操作结果
     */
    @Operation(summary = "新增菜单")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('" + PERMISSION_MENU_ADD + "')")
    @PreventDuplicateSubmit
    public Result<T> addMenu(@RequestBody MenuForm menuForm) {
        boolean result = menuService.saveMenu(menuForm);
        return Result.judge(result);
    }

    /**
     * 修改菜单
     *
     * @param id 菜单ID
     * @param menuForm 菜单表单数据
     * @return 操作结果
     */
    @Operation(summary = "修改菜单")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('" + PERMISSION_MENU_EDIT + "')")
    public Result<T> updateMenu(@PathVariable Long id, @RequestBody MenuForm menuForm) {
        boolean result = menuService.saveMenu(menuForm);
        return Result.judge(result);
    }

    /**
     * 删除菜单
     *
     * @param id 菜单ID
     * @return 操作结果
     */
    @Operation(summary = "删除菜单")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('" + PERMISSION_MENU_DELETE + "')")
    public Result<T> deleteMenu(@Parameter(description = "菜单ID，多个以英文(,)分割") @PathVariable Long id) {
        boolean result = menuService.deleteMenu(id);
        return Result.judge(result);
    }
}
